package com.hqhsc.module.login.controller;

import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hqhsc.module.common.domain.Menu;
import com.hqhsc.module.common.domain.UserRole;
import com.hqhsc.utils.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.hqhsc.module.common.service.CommonService;
import com.hqhsc.module.log.service.LogService;
import com.hqhsc.module.login.domain.User;
import com.hqhsc.module.login.service.LoginService;

@Controller
@RequestMapping("/login")
public class LoginController {
	final Logger logger = LoggerFactory.getLogger(LoginController.class);
	
	@Autowired
	private LoginService loginService;
	@Autowired
	private CommonService commonService;
	@Autowired
	private LogService logService;
	@Autowired
	private Properties appProps;
	

	@RequestMapping(value = "" , method = RequestMethod.GET)
	public String login(Model model , HttpServletRequest request , HttpServletResponse response) {
		User user = (User)request.getSession().getAttribute("user");
		if(user != null) {
			return "redirect:/login/returnindex";
		}
		
		return "/module/login";
	}
	
	@RequestMapping(value = "/index" , method = {RequestMethod.POST,RequestMethod.GET})
	public String index(Model model , HttpServletRequest request , HttpServletResponse response) {
		User user = (User)request.getSession().getAttribute("user");
		if(user != null) {
			return "redirect:/login/returnindex";
		}
		
		String name = request.getParameter("username");
		String pwd = request.getParameter("password");

        if(Utils.isBlank(name) || Utils.isBlank(pwd)) {
        	return "redirect:/login";
        }

		user = loginService.index(name , pwd);
		if(user != null) {
			model.addAttribute("user" , user);
			request.getSession().setAttribute("user", user);
			return "redirect:/login/returnindex";
		}
		return "redirect:/login";
	}
	
	/**
	 * 用户在线，但用户点击首页、操作不允许的菜单等
	 * 跳转至此接口
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/returnindex" , method = RequestMethod.GET)
	public String returnIndex(Model model , HttpServletRequest request , HttpServletResponse response) {
		User user = (User)request.getSession().getAttribute("user");
		if(user != null) {
            //获取用户角色列表
            List<UserRole> user_role_list = this.commonService.getUserRoleList(user.getUser_id());
            user.setRole_list(user_role_list);

            //设置用户菜单
            this.commonService.setUserMenu(user , user_role_list);

            model.addAttribute("user" , user);
            request.getSession().setAttribute("user", user);
            return "redirect:/manage/activity/index";
		}
		return "redirect:/login";
	}
}